package cn.bkw.ytkapi.common;

import cn.bkw.ytkapi.utils.helper;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * 拦截器
 */
public class Intercepter implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0,
                                HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
                           Object arg2, ModelAndView arg3) throws Exception {
		// TODO Auto-generated method stub
	}


	@Override
	public boolean preHandle(HttpServletRequest req, HttpServletResponse resp,
                             Object object) throws Exception {
		String url= req.getRequestURL().toString();
       if(url.indexOf("login")!=-1){
            return true;
        }
	       
		// TODO Auto-generated method stub
	    if (helper.ReadCookieMap(req).isEmpty()||helper.ReadCookieMap(req, "admin_usercookie").toString() == null || helper.ReadCookieMap(req, "admin_usercookie").toString().equals("") || helper.ReadCookieMap(req, "admin_usercookie").toString() == null) {
	    	
	    	resp.setContentType("text/html;charset=UTF-8");
	    	resp.setCharacterEncoding("UTF-8");
	        resp.setHeader("Cache-Control", "no-cache");
			PrintWriter out = resp.getWriter();
			out.println("<script language='javascript'>");
			out.println(" parent.location.href='login.jsp';");
			out.println("</script>");
			out.flush();
			out.close();
			return false;
		}else{
			return true;
		}
	}


}
